package com.bjpowerndoe.ylb.web.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.bjpowerndoe.ylb.common.AppTools;
import com.bjpowerndoe.ylb.map.BidProductNameMap;
import com.bjpowerndoe.ylb.map.BidProductNameMap;
import com.bjpowerndoe.ylb.web.enums.CodeEnum;
import com.bjpowerndoe.ylb.web.model.CommonResult;
import com.bjpowerndoe.ylb.web.model.dto.InvestRankDTO;
import com.bjpowerndoe.ylb.web.model.param.InvestParam;
import com.bjpowerndoe.ylb.web.model.vo.BidProductNameVO;
import com.bjpowerndoe.ylb.web.model.vo.InvestRankVO;
import com.bjpowerndoe.ylb.web.service.inter.InvestService;
import com.fasterxml.jackson.databind.JsonNode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 *
 */
@Api(tags = "投资模块")
@RequiredArgsConstructor
@RestController
public class InvestController {

    private final InvestService investService;

    @ApiOperation(value = "投资排行榜")
    @GetMapping("/invest/rank")
    public CommonResult showInvestRank(){
        List<InvestRankDTO> investRank = investService.getInvestRank();
        List<InvestRankVO> investRankVOList = BeanUtil.copyToList(investRank, InvestRankVO.class);
        return CommonResult.ok(investRankVOList);
    }

    @ApiOperation(value = "投资，购买理财产品")
    @PostMapping("/invest/buy")
    public CommonResult buyProduct(@RequestHeader("uid") Integer uid,
                                   @RequestBody InvestParam investParam){
        //参数检查
        CommonResult commonResult = CommonResult.fail(CodeEnum.REQUEST_PARAM_ERR);
        if( investParam.checkData()){
            //调用service做投资
            CodeEnum codeEnum = investService.buyProduct(uid,
                    investParam.getProductId(), investParam.getInvestMoney());
            //更新投资排行榜
            if( CodeEnum.OK == codeEnum ){
                investService.modifyInvestRank(uid,investParam.getInvestMoney() );
            }


            commonResult.setCodeEnum(codeEnum);
        }

        return commonResult;
    }


    @ApiOperation("查询更多投资记录")
    @GetMapping("/invest/more")
    public CommonResult queryBidListByUid(@RequestHeader("uid") Integer uid,
                                          @RequestParam(required = false) Integer pageNo,
                                          @RequestParam(required = false,defaultValue = "6") Integer pageSize){
        CommonResult commonResult = CommonResult.fail(CodeEnum.REQUEST_PARAM_ERR);
        if( uid > 0 ){
            pageNo = AppTools.defaultPageNo(pageNo);
            pageSize = AppTools.defaultPageSize(pageSize);
            List<BidProductNameMap> bidList = investService.queryBidListByUid(uid, pageNo, pageSize);
            commonResult = CommonResult.ok(BeanUtil.copyToList(bidList, BidProductNameVO.class));
        }
        return commonResult;
    }



}
